﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Caching;
using System.Web;

namespace ArkEC.CoreCom.CacheStore
{
    /// <summary>
    /// Web缓存辅助处理类
    /// </summary>
    public class WebCacheHelper
    {
        #region 变量

        /// <summary>
        /// 全局缓存的过期时间（分钟）
        /// </summary>
        private static int GlobalCacheTimeLimit = 10;

        #endregion

        #region 添加或修改缓存值/条目

        /// <summary>
        /// 添加或修改缓存值
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="cacheLifeCycle"></param>
        /// <param name="value"></param>
        public static void SetItem(string cacheName, CacheLifeCycleEnum cacheLifeCycle, object value)
        {
            if (cacheLifeCycle == CacheLifeCycleEnum.Session)
            {
                HttpContext.Current.Session.Add(cacheName, value);
            }
            else
            {
                HttpContext.Current.Cache.Insert(cacheName, value, null, Cache.NoAbsoluteExpiration,
                        new TimeSpan(0, GlobalCacheTimeLimit, 0));
            }
        }

        /// <summary>
        /// 添加或修改缓存条目
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="cacheLifeCycle"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetItem(string cacheName, CacheLifeCycleEnum cacheLifeCycle, string key, object value) 
        {
            if (cacheLifeCycle == CacheLifeCycleEnum.Session)
            {
                Dictionary<string, object> cacheObj = HttpContext.Current.Session[cacheName] as Dictionary<string, object>;
                if (cacheObj == null)
                {
                    cacheObj = new Dictionary<string, object>();
                    HttpContext.Current.Session.Add(cacheName, cacheObj);
                }

                cacheObj.Add(key, value);
            }
            else
            {
                Dictionary<string, object> cacheObj = HttpContext.Current.Cache[cacheName] as Dictionary<string, object>;
                if (cacheObj == null)
                {
                    cacheObj = new Dictionary<string, object>();
                    HttpContext.Current.Cache.Add(cacheName, cacheObj, null, Cache.NoAbsoluteExpiration,
                        new TimeSpan(0, GlobalCacheTimeLimit, 0), CacheItemPriority.Default, null);
                }

                cacheObj.Add(key, value);
            }
        }

        #endregion

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="cacheName"></param>
        public static void RemoveCache(string cacheName)
        {
            HttpContext.Current.Session.Remove(cacheName);
            HttpContext.Current.Cache.Remove(cacheName);
        }
        
        /// <summary>
        /// 清除缓存条目（只支持数据字典结构）
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="key"></param>
        public static void RemoveCacheItem(string cacheName, string key)
        {
            Dictionary<string, object> cacheObj = HttpContext.Current.Session[cacheName] as Dictionary<string, object>;
            if (cacheObj == null)
            {
                cacheObj = HttpContext.Current.Cache[cacheName] as Dictionary<string, object>;
            }

            if (cacheObj != null)
            {
                cacheObj.Remove(key);
            }
        }

        /// <summary>
        /// 获取缓存条目
        /// </summary>
        /// <param name="cacheType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetItem(CacheType cacheType, string key)
        {
            Dictionary<string, object> cacheObj = null;
            if (HttpContext.Current.Session != null)
            {
                cacheObj = HttpContext.Current.Session[cacheType.CacheName] as Dictionary<string, object>;
            }
            if (cacheObj == null)
            {
                cacheObj = HttpContext.Current.Cache[cacheType.CacheName] as Dictionary<string, object>;
            }

            //自动获取数据源
            if (cacheObj == null)
            {
                cacheObj = cacheType.ReadCache();
            }

            object obj = null;
            if (cacheObj.ContainsKey(key))
            {
                obj = cacheObj[key];
            }

            return obj;
        }
    }
}
